package com.ruoyi.web.controller.starbucks;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.starbucks.domain.QueryStarbucksAccount;
import com.ruoyi.starbucks.domain.StarbucksAccount;
import com.ruoyi.starbucks.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 星巴克账号Controller
 *
 * @author ruoyi
 * @date 2021-01-03
 */
@ApiOperation("星巴克注册")
@RestController
@RequestMapping("/starbucks/reg")
public class StarbucksRegController extends BaseController
{
    @Autowired
    private IStarbucksAccountService starbucksAccountService;

    /**
     * 获取新手机号
     */
    @ApiOperation("获取新手机号")
    @GetMapping("/GetPhoneNumber")
    public AjaxResult GetPhoneNumber(String deviceName) throws InterruptedException {
        String phoneNo="";
        if(RuoYiConfig.getSmsChannel().equals("SrmService5")){
            phoneNo=SrmService5.GetPhoneNo();
        }
        else if(RuoYiConfig.getSmsChannel().equals("SrmService7")){
            phoneNo=SrmService7.GetPhoneNo();
        }
        else {
            phoneNo = SrmService6.GetPhoneNoLoop(100);
        }
        QueryStarbucksAccount queryStarbucksAccount=new QueryStarbucksAccount();
        queryStarbucksAccount.setCellPhone(phoneNo);
        List<StarbucksAccount> list=starbucksAccountService.selectStarbucksAccountList(queryStarbucksAccount);
        if(list==null || list.size()==0){
            logger.info("操作成功，获取手机号{}为新手机号",phoneNo);
            return AjaxResult.success("操作成功",phoneNo);
        }
        logger.info("操作成功，手机号已存在");
        return AjaxResult.success("操作成功","");
    }


    /**
     * 根据手机号获取短信
     */
    @ApiOperation("获取短信")
    @GetMapping("/GetMessage")
    public AjaxResult GetMessage(String phoneNo,String deviceName)
    {
        String data ="";
        if(RuoYiConfig.getSmsChannel().equals("SrmService5")){
            data = SrmService5.GetMessage(phoneNo);
        }
        else if(RuoYiConfig.getSmsChannel().equals("SrmService7")){
            data=SrmService7.GetMessage(phoneNo);
        }
        else{
            data = SrmService6.GetMessage(phoneNo);
        }
        return AjaxResult.success("操作成功",data);
    }

    /**
     * 将手机号加入黑名单
     */
    @ApiOperation("将手机号加入黑名单")
    @GetMapping("/AddToBlackList")
    public AjaxResult AddToBlackList(String phoneNo)
    {
        String data="";
        if(RuoYiConfig.getSmsChannel().equals("SrmService5")){
            SrmService5.AddToBlackList(phoneNo);
        }
        else if(RuoYiConfig.getSmsChannel().equals("SrmService7")){
            data = SrmService7.ReturnState(phoneNo,"2");
        }
        else{
            data = SrmService6.ReturnState(phoneNo,"2");
        }
        return AjaxResult.success(data);
    }

    /**
     * 新增星巴克账号
     */
    @ApiOperation("新增星巴克账号")
    @GetMapping("/UpLoadCkFromDevice")
    public AjaxResult UpLoadCkFromDevice(Boolean isNew, String token, String phoneNo, String deviceName )
    {
        AjaxResult ajaxResult;
        token=token.replaceAll("Bearer ","");
        if(StringUtils.isNotEmpty(token)){
            ajaxResult=starbucksAccountService.insertStarbucksAccount(token,isNew?"1":"0",deviceName,phoneNo);
            logger.info("新增账号信息成功");
            if(RuoYiConfig.getSmsChannel().equals("SrmService6")){
                SrmService6.ReturnState(phoneNo,"1");
            }
            if(RuoYiConfig.getSmsChannel().equals("SrmService7")){
                  SrmService7.ReturnState(phoneNo,"1");
            }
            //更新token信息
            Thread t = new Thread(new Runnable(){
                public void run(){
                    StarbucksAccount starbucksAccount = (StarbucksAccount) ajaxResult.get("data");
                    starbucksAccountService.updateInfoById(starbucksAccount.getId());
                    logger.info("同步账号的全部信息");
                }});
            t.start();
            return ajaxResult;
        }else{
            ajaxResult=AjaxResult.error("token为空");
            return ajaxResult;
        }
    }

    @ApiOperation("getAnswer")
    @GetMapping("/getAnswer")
    public AjaxResult getAnswer(){
        return starbucksAccountService.getAnswer();
    }

    @ApiOperation("answer")
    @GetMapping("/answer")
    public AjaxResult answer(String token){
        return starbucksAccountService.answer(token);
    }
}
